// pages/hall/hall.js
const app = getApp()
var config = require('../../config.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    login: [{
      'name': '个人登录（我要找活)',
      'status': true
    }, {
      'name': "企业登录（我要找人)",
      'status': false
    }],
    radioIndex: 0,
  },

  showModal(e) {
    if (!wx.getStorageSync('sessionKey')) {
      wx.navigateTo({
        url: '../authorizedLogin/authorizedLogin',
      })
    } else {

      var userPhone = wx.getStorageSync("phoneNumber");
      var that = this;
      if (userPhone == "" || userPhone == null || userPhone == undefined) {
        app.tip("授权之后才可以登录", "none");
        return false;
      }
      console.info(userPhone)

      var userType = '1';
      wx.request({
        url: config.loginUrl,
        method: 'POST',
        header: {
          "content-type": "application/x-www-form-urlencoded"
        },
        data: {
          "userPhone": userPhone,
          "userType": userType
        },
        success: function (result) {
          console.info("登录");
          console.info(result);
          var data = result.data.object;
          if (data != null) {
            wx.setStorageSync("companyInfo", null);
            wx.setStorageSync("uid", 0);
            wx.setStorageSync("workerInfo", data);
            that.setData({
              uid: 0,
              modalName: null
            })

          } else {
            if (result.data.code == '201') {
              app.tip("该账号是企业账号,不可以登录个人系统", "none");
            } else {
              wx.navigateTo({
                url: '../message/message',
              })
            }
          }
        },
        fail: function ({ errMsg }) {
          wx.hideLoading()
          app.tip(errMsg, 'none')
        }
      })

      // this.setData({
      //   modalName: e.currentTarget.dataset.target
      // })
    }

  },
  /**
   * 接单
   */
  receiptOrder(e) {
    var that = this;
    var workerInfo=wx.getStorageSync("workerInfo");
    var workerCode ="";
    if (workerInfo!=null){
      workerCode = workerInfo.workerCode;
    }
    var orderCode = that.data.orderCode;
    //接单参数
    let searchData = {
      orderCode: orderCode,
      personnelCode: workerCode
    }
    wx.request({
      url: config.queryIndexReceiptOrder,
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      data: searchData,
      success: function (result) {
        console.info("接单返回结果");
        console.info(result);
        if (result.data.success == true) {
          app.tip("接单成功", "none");
          setTimeout(function () {
            wx.navigateTo({
              url: '/pages/index/index',
            })
          }, 1000) //延迟时间
         
        } else {
          app.tip(result.data.message, "none");
        }
        
       
      },
      fail: function ({ errMsg }) {

      },
    });
   
  },
  /**
 * 返回
 */
  returnOrder(e) {
    wx.navigateTo({
      url: '/pages/index/index',
    })
  },
  /**
   * 订单完成
   */
  editOrderStatus(e) {
    var that = this;
    var workerInfo = wx.getStorageSync("workerInfo");
    var workerCode = "";
    if (workerInfo != null) {
      workerCode = workerInfo.workerCode;
    }
    var orderCode = that.data.orderCode;
    //订单完成参数
    let searchData = {
      orderCode: orderCode,
      personnelCode: workerCode
    }
    wx.request({
      url: config.queryIndexEditOrderStatus,
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      data: searchData,
      success: function (result) {
        console.info("订单完成返回结果");
        console.info(result);
        if (result.data.success == true) {
          app.tip("保存成功", "none");
          setTimeout(function () {
            wx.navigateTo({
              url: '/pages/index/index',
            })
          }, 1000) //延迟时间
        } else {
          app.tip(result.data.message, "none");
        }


      },
      fail: function ({ errMsg }) {

      },
    });

  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  radioChange: function (e) {
    var that = this;
    var index = e.detail.value;
    that.setData({
      radioIndex: index
    })
  },
  confi: function () {
    var userPhone = wx.getStorageSync("phoneNumber");
    var that = this;
    var radioIndex = that.data.radioIndex;
    if (userPhone == "" || userPhone == null || userPhone == undefined) {
      app.tip("授权之后才可以登录", "none");
      return false;
    }
    console.info(userPhone)
    if (radioIndex == 1) {
      console.info("企业登录")
      var userType = '2';
      wx.request({
        url: config.loginUrl,
        method: 'POST',
        header: {
          "content-type": "application/x-www-form-urlencoded"
        },
        data: {
          "userPhone": userPhone,
          "userType": userType
        },
        success: function (result) {
          console.info(result);
          var data = result.data.object;
          console.info(data);
          if (data != null) {
            console.info("有");
            wx.setStorageSync("workerInfo", null);
            wx.setStorageSync("uid", 1);
            wx.setStorageSync("companyInfo", data);
            that.setData({
              uid: 1,
              modalName: null
            })

          } else {
            console.info("无");
            if (result.data.code == '201') {
              app.tip("该账号是个人账号,不可以登录企业系统", "none");
            } else {
              wx.navigateTo({
                url: '../member/member',
              })
            }
          }
        },
        fail: function ({ errMsg }) {
          wx.hideLoading()
          app.tip(errMsg, 'none')
        }
      })

    } else {
      console.info("个人登录")
      var userType = '1';
      wx.request({
        url: config.loginUrl,
        method: 'POST',
        header: {
          "content-type": "application/x-www-form-urlencoded"
        },
        data: {
          "userPhone": userPhone,
          "userType": userType
        },
        success: function (result) {
          console.info(result);
          var data = result.data.object;
          if (data != null) {
            wx.setStorageSync("companyInfo", null);
            wx.setStorageSync("uid", 0);
            wx.setStorageSync("workerInfo", data);
            that.setData({
              uid: 0,
              modalName: null
            })

          } else {
            if (result.data.code == '201') {
              app.tip("该账号是企业账号,不可以登录个人系统", "none");
            } else {
              wx.navigateTo({
                url: '../message/message',
              })
            }
          }
        },
        fail: function ({ errMsg }) {
          wx.hideLoading()
          app.tip(errMsg, 'none')
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.info("uid:"+wx.getStorageSync("uid"));
    var that = this;
    var orderCode = options.orderCode;
    that.queryInfo(orderCode);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

 /**
   * 获取信息
   */
  queryInfo: function (orderCode) {
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    let searchData = {
      orderCode: orderCode
    }
    var uid=that.data.uid;
    console.info("hall uid" + config.queryIndexOrderInfo);
    console.info("asdasdasdasdasd"+config.queryIndexOrderInfo);
    wx.request({
      url: config.queryIndexOrderInfo,
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      data: searchData,
      success: function (result) {
        wx.hideLoading();
        console.info("首页订单详情询返回结果");
        console.info(result);
        var data = result.data.object;
        var company = data.company;
        var order = data;
        var settlement = data.settlement;
        var worker = data.worker;
        var saveType="";
    
        var uid=wx.getStorageSync("uid");
        if (uid != "1" && uid != "0"){
          uid=2;
        }
        console.info("uid:" + uid);
        console.info(data);
        that.setData({
          uid: uid,
          status: order.orderStatus,
          companyCode: company.companyCode,
          companyName: company.companyName,
          content: company.content,
          industryTypeName: data.industryType.industryTypeName,
          linkMan: company.linkMan,
          companyPhone: company.phone,
          companyAddress: company.address,
          orderCode: order.orderCode,
          orderTime: order.orderTime,
          orderTitle: order.orderTitle,
          taskDescription: order.taskDescription,
          qualifications: order.qualifications,
          effectiveTime: order.effectiveTime,
          workAddress: order.workAddress,
          orderStatus: order.orderStatus,
          businessTypeName: data.businessType.businessTypeName,
          billingUnit: order.billingUnit,
          unitPrice: order.unitPrice,
          generalTask: order.generalTask,
          demandPopulation: order.demandPopulation,
          totalCost: order.totalCost,
          settlementStatus: order.settlementStatus,
          tradeType: '任务详情'
        });

      },
      fail: function ({ errMsg }) {
        console.info("订单详情查询失败");
        wx.hideLoading();
      },
    });
  }

  
})